package com.ag.oa.core.service.impl;

import com.ag.oa.core.dao.IMessageDao;
import com.ag.oa.core.dao.IProjectDao;
import com.ag.oa.core.dao.IUserDao;
import com.ag.oa.core.entity.vo.ProjectVO;
import com.ag.oa.core.entity.vo.UserVO;
import com.ag.oa.core.service.IProjectService;
import com.ag.oa.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Author:   wjc
 * Date:     2021/10/18
 * Description:
 */
@Service("projectServiceImpl")
public class ProjectServiceImpl implements IProjectService {

    @Autowired
    private IProjectDao projectDao;

    @Autowired
    private IUserDao userDao;

    @Autowired
    private IMessageDao messageDao;

    @Override
    public void createProject(ProjectVO projectVO) {

        projectDao.createProject(projectVO);
        projectDao.createProjectFirstUser(projectVO);

        // 保存到成员列表中
        projectDao.saveMemberUser(projectVO.getProjectId(), projectVO.getUserId()
                , projectVO.getUserId(), 1);
    }

    @Override
    public String getProjectManagerFlag(Integer projectId, Integer userId) {

        Integer dataCount = projectDao.getProjectManagerFlag(projectId, userId);
        if (dataCount>0) {
            return "y";
        } else {
            return "n";
        }
    }

    @Override
    public List<ProjectVO> findProjectList(Integer projectStatus, Integer projectUserId, Integer projectId) {

        return projectDao.findProjectList(projectStatus, projectUserId, projectId);
    }

    @Override
    public ProjectVO getProjectById(Integer projectId) {

        return projectDao.getProjectById(projectId);
    }

    @Override
    public List<Map> getAllMyProjectsData(Integer userId, Integer page, Integer pageSize) {

        if (page==null) {
            page = 0;
        }

        if (pageSize==null) {
            pageSize = 0;
        }
        return projectDao.getUserProjectsData(userId, page * pageSize, pageSize);
    }

    @Override
    public void updateProjectStatus(ProjectVO vo) {

        projectDao.updateProjectStatus(vo);
    }

    @Override
    public void updateProjectDesc(ProjectVO vo) {

        projectDao.updateProjectDesc(vo);
    }

    @Override
    public void updateConstitution(ProjectVO vo) {

        projectDao.updateConstitution(vo);
    }

    @Override
    public List<ProjectVO> findProjectUserById(Integer projectId) {

        return projectDao.findProjectUserById(projectId);
    }

    /**
     * 删除合作社负责人
     */
    @Override
    public void deleteProjectUserById(Integer projectId, Integer userId) {

        projectDao.deleteProjectUserById(projectId, userId);  // 从负责人表中删除
        projectDao.deleteMemberUser(projectId, userId);  // 从成员表中删除
    }

    @Override
    public List<UserVO> getProjectUserCheckDataList(String username, Integer projectId, Integer limitCount) {

        return projectDao.getProjectUserCheckDataList(username, projectId, limitCount);
    }

    /**
     * 添加负责人
     */
    @Override
    public void createProjectUsers(ProjectVO projectVO) {

        projectDao.createProjectUsers(projectVO);

        // 在成员表中添加负责人 (2022-02-10)
        for (Integer userId: projectVO.getUserIds()) {
            int count = projectDao.getMemberUserCount(projectVO.getProjectId(), userId);
            if (count==0) {
                projectDao.saveMemberUser(projectVO.getProjectId(), userId, projectVO.getCreatedBy(), 1);
            }
        }
    }

    @Override
    public List<Map> findProjectAllMembers(int projectId) {

        return projectDao.getAllMemberList(projectId);
    }

    @Override
    public List<Map> getProjectAddableMembers(Integer projectId, Integer userId, Integer page, Integer pageSize
            , String username) {

        // 已添加的用户
        List<Object> list = new ArrayList<>();
        list.add(userId);  // 不显示自己

        List<Map> memberList = projectDao.getAllMemberList(projectId);
        for (Map map: memberList) {
            list.add(map.get("id"));
        }

        if (username!=null) {
            username = username.trim();
        }
        return projectDao.getAddableMemberUsers(list, page * pageSize, pageSize, username);
    }

    /**
     * 保存合作社成员
     * 6. 可以将所有用户都添加为成员（不局限为社员）但需用户同意，才能添加成功。决策社员无此限制。
     */
    @Override
    public void saveProjectMemberUsers(Integer projectId, Integer userId, String checkedUserStr) {

        // 获取当前添加者的用户信息
        UserVO vo = userDao.getById(userId);
        List<Integer> toUserList = new ArrayList<>();
        int status = 0;  // 需要用户处理

        String[] userArr = checkedUserStr.split(",");
        for (String memUserId: userArr) {
            // 非决策社员用户添加其他用户为社员时, 需要通知用户
            if (vo.getVip()==100) {
                // 决策社员
                int count = projectDao.getMemberUserCount(projectId, memUserId);
                if (count==0) {
                    projectDao.saveMemberUser(projectId, memUserId, userId, 0);
                }
                status = 2;  // 忽略 (不需要用户处理)
            }

            // 检查消息是否已存在
//            messageDao.get
            toUserList.add(Integer.parseInt(memUserId));
        }

        // 通知用户
        messageDao.saveNewMessage(6, userId, toUserList, null
                , null, null, null, projectId, status, null);
    }

    @Override
    public void delProjectMemberUser(Integer projectId, Integer userId) {

        // 从合作社负责人表和成员表中删除

        // 删除积分记录
    }

    @Override
    public void saveProjectMemberScore(Integer projectId, Integer userId, Integer addUserId, Integer score
            , String description) {

        String nowTime = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        userDao.saveNewScoreRecord(projectId, userId, addUserId, score, description, 0, nowTime);
    }

    @Override
    public List<Map> getUserAllProjectScoreList(Integer userId) {

        return projectDao.getUserProjectsScoreList(userId);
    }

    @Override
    public List<Map> getUserProjectScoreDetail(Integer projectId, Integer userId) {

        return projectDao.getUserProjectScoreRecordList(projectId, userId);
    }

    @Override
    public Map<String, Object> getProjectScoreInfo(Integer scoreId) {

        return projectDao.getScoreDetailInfo(scoreId);
    }

}
